const BaseRest = require('./rest.js');

const qiniu = require('qiniu');
const qiniuConfig = think.config('qiniu');
const accessKey = qiniuConfig['ak'];
const secretKey = qiniuConfig['sk'];
const bucket = qiniuConfig['bucket'];
const domain = qiniuConfig['domain'];
// const key = qiniuConfig['key'];

module.exports = class extends BaseRest {
  postAction() {
    const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
    const options = {
      scope: bucket, // + ':'+key
    };
    const putPolicy = new qiniu.rs.PutPolicy(options);
    const uploadToken = putPolicy.uploadToken(mac);
    
    const data = {'token': uploadToken, 'domain': domain};
    think.logger.debug(data);
    
    return this.success(data);
  }
};
